from PySide6.QtCore import Qt
from PySide6.QtGui import QFont
from PySide6.QtWidgets import QWidget, QVBoxLayout, QStyle, QPushButton, QLabel, QHBoxLayout, QLineEdit

import DBAccessor


class Register_Dialog(QWidget):
    RES_X: int = None
    RES_Y: int = None
    DBAccessor: DBAccessor = None
    TitlePointSize: int = None
    AttributePointSize: int = None
    LineEditPointSize: int = None
    Uid: int = None
    Parent = None

    def __init__(self, parent, res_x: int, res_y: int, dbaccessor: DBAccessor):  # Surface 684*456
        super().__init__()
        self.RES_X = res_x
        self.RES_Y = res_y
        self.DBAccessor = dbaccessor
        self.Parent = parent

        # 初始化参数
        self.TitlePointSize = int(res_x / 75)
        self.AttributePointSize = int(res_x / 100)

        # 设置窗口属性
        self.setWindowIcon(self.style().standardPixmap(QStyle.StandardPixmap.SP_TrashIcon))  # 图标
        self.setWindowTitle("Magic Calendar: Register Dialog (*^▽^*)")  # 标题
        self.setFixedSize(int(res_x / 4), int(res_y / 4))  # 最大窗口大小

        # 标题部分
        temp_font = QFont("微软雅黑", self.TitlePointSize)
        temp_font.setBold(True)
        self.label_title = QLabel("输入你的账号信息\n\\(*^▽^*)/")
        self.label_title.setFont(temp_font)
        self.label_title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_title.setMinimumHeight(self.TitlePointSize * 5)

        # 昵称输入
        self.layout_h_nickname = QHBoxLayout()  # 输入布局
        temp_font.setPointSize(self.AttributePointSize)
        temp_font.setBold(False)
        # Label
        self.label_nickname = QLabel("昵称 ")
        self.label_nickname.setFont(temp_font)
        self.label_nickname.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_nickname.setMaximumHeight(self.AttributePointSize * 2)
        self.layout_h_nickname.addWidget(self.label_nickname)
        # LineEdit
        self.lineedit_nickname = QLineEdit()
        self.lineedit_nickname.setPlaceholderText("这里输入昵称！")
        self.lineedit_nickname.setFont(temp_font)
        self.layout_h_nickname.addWidget(self.lineedit_nickname)

        # 密码输入
        self.layout_h_password = QHBoxLayout()  # 输入布局
        # Label
        self.label_password = QLabel("密码 ")
        self.label_password.setFont(temp_font)
        self.label_password.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label_password.setMaximumHeight(self.AttributePointSize * 2)
        self.layout_h_password.addWidget(self.label_password)
        # LineEdit
        self.lineedit_password = QLineEdit()
        self.lineedit_password.setPlaceholderText("这里输入密码！")
        self.lineedit_password.setFont(temp_font)
        self.lineedit_password.setEchoMode(QLineEdit.EchoMode.Password)
        self.layout_h_password.addWidget(self.lineedit_password)

        # 按钮
        temp_font.setPointSize(int(self.AttributePointSize * 5 / 6))
        self.button_register = QPushButton()
        self.button_register.setText("注册")
        self.button_register.setFont(temp_font)
        self.button_register.clicked.connect(self.handle_register)

        # 总布局
        layout_main = QVBoxLayout(self)
        layout_main.addWidget(self.label_title)
        layout_main.addLayout(self.layout_h_nickname)
        layout_main.addLayout(self.layout_h_password)
        layout_main.addWidget(self.button_register)

    def handle_register(self):
        nickname = self.lineedit_nickname.text()
        password = self.lineedit_password.text()
        # 处理完成过注册：处理信息；未完成过：提示不准再点
        if password == "":
            self.label_title.setText("密码不能为空！！！")
        elif nickname == "":
            self.label_title.setText("昵称不能为空！！！")
        else:
            password_md5 = DBAccessor.encode_password(password)  # 加密密码
            self.Uid = self.DBAccessor.insert_user(nickname, password_md5)
            if self.Uid < 0:
                self.label_title.setText("注册失败！")
            else:
                self.label_title.setText("注册成功！\n请记住你的UID：" + str(self.Uid))
                self.lineedit_nickname.setReadOnly(True)
                self.lineedit_password.setReadOnly(True)
                self.button_register.setText("返回")
                self.button_register.clicked.disconnect(self.handle_register)
                self.button_register.clicked.connect(self.close)


    def close(self):
        self.Parent.lineedit_account.setText(str(self.Uid))
        super().close()
